//###############
//FILE : FireRequest.java
//WRITER : Daniel Lowengrub, daniel_i_l, 320687098
//EXERCISE : oop ex3 2011
//DESCRIPTION:
//See below
//###############
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package oop.ex3.nameserver;

import oop.ex3.communication.ServerStatus;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import oop.ex3.protocol.sessions.NameGoAwaySession;

/**
 * This handles the FIRESERVERS request as specified in the assignment.
 * @author daniel
 */
public class FireRequest extends FileManagerSpecificRequest{
    private NameGoAwaySession _goAwaySession;
    private ServerStatus _status;

    /**
     * Create a new request initialized with a NS database which is passed to the super class.
     * The opening session is a NameGoAwaySession.
     * @param nsDb The nameserver database.
     */
    public FireRequest(NameServerDatabase nsDb, ServerStatus status) {
        super(nsDb, new NameGoAwaySession());
        _goAwaySession = (NameGoAwaySession)_openingSession;
        _status = status;
    }


    @Override
    public boolean serveRequest(DataInputStream inputStream, DataOutputStream outputStream) {
        //System.out.println("serving fire request");
        //first run the go away session
        if (!_goAwaySession.executeSession(inputStream, outputStream)) {
            return false;
        }

        //if we didn't find the end  then there was an error
        if (!_endSession.executeSession(inputStream, outputStream)) {
            return false;
        }
        
        _status.stopServer();
        return false;
    }

    @Override
    public NameServerRequest getCopy() {
        FireRequest copy = new FireRequest(_nameServerDb, _status);
        return copy;
    }

}
